// Copyright (c) Appeon. All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. 
// You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0.

using System.Collections.Generic;

namespace SnapObjects.Data.MySql
{
    internal class MySqlUpdateStatementGenerator : SqlUpdateStatementGenerator
    {
        public MySqlUpdateStatementGenerator(
            ISqlStatementFactory factory,
            ISqlGenerationHelper helper,
            ITypeMapper typeMapper) : base(factory, helper, typeMapper)
        {
        }


        protected override void ParseUpdateSet(List<SqlToken> tokens, SqlUpdateStatement update)
        {
            tokens.Add(new SqlKeywordToken(SqlKeywords.UPDATE));
            tokens.Add(SqlWhitespaceToken.Whitespace);

            if (update.From.Count > 0)
            {
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.AddRange(this.SpliceClause(update.From));

                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.Add(new SqlKeywordToken(SqlKeywords.SET));
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.AddRange(this.SpliceClause(update.Set));
            }
            else
            {
                tokens.AddRange(this.SpliceClause(update.Table));
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.Add(new SqlKeywordToken(SqlKeywords.SET));
                tokens.Add(SqlWhitespaceToken.Whitespace);
                tokens.AddRange(this.SpliceClause(update.Set));
            }
        }
    }
}
